package com.android.matmanual;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
//for girdview
import com.android.internal.os.storage.ExternalStorageFormatter;

public class FactoryTestListActivity extends Activity implements
		OnItemClickListener {
	private static final String TAG = "FactoryTest";
	GridView mGridView;// for girdview ListView mListView;
	FactoryTestListAdapter mAdapter;
	WakeLock mWakeLock;

	private ArrayList<GridViewItems> mGridViewItems;
	private EngSqlite mEngSqlite;

	String[] mString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.e(TAG, "helye onCreate ");
		setContentView(R.layout.factory_grid_test);// for girdview
		//setContentView(R.layout.factory_test);
		mString = new String[] {
				getResources().getString(R.string.bulid_number),
				// getResources().getString(R.string.rf_test),
				getResources().getString(R.string.receive_mic),
				getResources().getString(R.string.handset),
				getResources().getString(R.string.sdcard),
				getResources().getString(R.string.rtctest),
				getResources().getString(R.string.keypad),
				getResources().getString(R.string.vibrator),
				getResources().getString(R.string.screen_dead_point),
				getResources().getString(R.string.lcd),
				getResources().getString(R.string.speaker),
				getResources().getString(R.string.battery),
				getResources().getString(R.string.headset),
				getResources().getString(R.string.fm_radio),
				getResources().getString(R.string.touch_screen),
				getResources().getString(R.string.gsensor),
				getResources().getString(R.string.front_camera),
				getResources().getString(R.string.camera),
				getResources().getString(R.string.wifi),
				getResources().getString(R.string.bluetooth),
				getResources().getString(R.string.sim_test),
				getResources().getString(R.string.multipoint_test),
				getResources().getString(R.string.gps_test),
				getResources().getString(R.string.proximity_test),
				// getResources().getString(R.string.light_test),
				// getResources().getString(R.string.magnetic_field_test),
				getResources().getString(R.string.flashlight_test),
				// getResources().getString(R.string.cputemperature_test),
				getResources().getString(R.string.TestResult),
				//getResources().getString(R.string.exit)
				getResources().getString(R.string.factoryreset)
		};
		
		
		setTitle("LAVA Factory Test");
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
				"XYTEST");
		mWakeLock.acquire();
		Settings.System.putInt(getContentResolver(), "IsMatTest", 1);

		// for girdview 
		//mListView = (ListView)findViewById(R.id.factory_test_list);
		mGridView = (GridView) findViewById(R.id.factory_test_grid);
		mGridView.setNumColumns(2);
		mGridView.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		mEngSqlite = EngSqlite.getInstance(this);
		mGridViewItems = new ArrayList<GridViewItems>();
		int itemCount = mString.length;
		for (int i = 0; i < itemCount; i++) {
			GridViewItems item = new GridViewItems();
			item.textTitle = mString[i];
			item.itemID = i;
			mGridViewItems.add(item);
		}

		mAdapter = new FactoryTestListAdapter(this, mString, mString.length,
				mGridViewItems);
		mGridView.setAdapter(mAdapter);// for girdview
		// mListView.setAdapter(mAdapter);

		mGridView.setOnItemClickListener(this);// for girdview
		// mListView.setOnItemClickListener(this);

		TestControl.Init(TestControl.NORMAL_STATE);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		int tag = (Integer) arg1.getTag();
		Intent intent = null;
		int idx = tag - 1;
		switch (tag) {
		case 1:
			intent = new Intent(FactoryTestListActivity.this, VersionTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		/*
		 * case 2: // sendBroadcast( new Intent(Intents.SECRET_CODE_ACTION,
		 * Uri.parse("android_secret_code://" + "RFTest"))); //startActivity(new
		 * Intent("android.intent.action.RFCalibrateInfo"));
		 * 
		 * break;
		 */
		case 2:
			intent = new Intent(FactoryTestListActivity.this, ReceiveTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent); // lifei
			/*
			 * Intent intent1 = new Intent(Intent.ACTION_HEADSET_PLUG);
			 * intent1.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			 * intent1.putExtra("state", 0); intent1.putExtra("name", "h2w");
			 * intent1.putExtra("microphone", 1); sendBroadcast(intent1);
			 * //startActivity(new
			 * Intent("android.intent.action.PhoneLoopback")); sendBroadcast(
			 * new Intent(Intents.SECRET_CODE_ACTION,
			 * Uri.parse("android_secret_code://" + "PhoneLoopbackTest")));
			 */
			break;
		case 3:
			intent = new Intent(FactoryTestListActivity.this, HandsetTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 4:
			intent = new Intent(FactoryTestListActivity.this, SdcardTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 5:
			intent = new Intent(FactoryTestListActivity.this, RTCTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 6:
			intent = new Intent(FactoryTestListActivity.this, KeypadTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 7:
			intent = new Intent(FactoryTestListActivity.this,
					VibratorTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 8:
			intent = new Intent(FactoryTestListActivity.this,
					ScreenDeadPointTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 9:
			intent = new Intent(FactoryTestListActivity.this, LCDTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 10:
			intent = new Intent(FactoryTestListActivity.this, SpeakerTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 11:
			intent = new Intent(FactoryTestListActivity.this,
					ChargingTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 12:
			intent = new Intent(FactoryTestListActivity.this, HeadsetTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent); // lifei
			/*
			 * Intent intent2 = new Intent(Intent.ACTION_HEADSET_PLUG);
			 * intent2.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
			 * intent2.putExtra("state", 0); intent2.putExtra("name", "h2w");
			 * intent2.putExtra("microphone", 1); sendBroadcast(intent2);
			 * //startActivity(new
			 * Intent("android.intent.action.HeadsetLoopback")); sendBroadcast(
			 * new Intent(Intents.SECRET_CODE_ACTION,
			 * Uri.parse("android_secret_code://" + "HeadsetLoopbackTest")));
			 */
			break;
		case 13:
			intent = new Intent(FactoryTestListActivity.this, FMTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 14:
			intent = new Intent(FactoryTestListActivity.this, Touch_Test.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 15:
			intent = new Intent(FactoryTestListActivity.this, GravityTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 16:
			intent = new Intent(FactoryTestListActivity.this,
					FrontCameraTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 17:
			intent = new Intent(FactoryTestListActivity.this, CameraTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 18:
			intent = new Intent(FactoryTestListActivity.this, WifiTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 19:
			intent = new Intent(FactoryTestListActivity.this,
					BluetoothTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 20:
			intent = new Intent(FactoryTestListActivity.this, SimTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 21:
			intent = new Intent(FactoryTestListActivity.this,
					MultipleTPTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 22:
			startActivity(new Intent(FactoryTestListActivity.this,
					GpsTest.class));
			break;
		/*
		 * case 21: startActivity(new
		 * Intent(FactoryTestListActivity.this,ProximityTest.class)); break;
		 * case 22: startActivity(new
		 * Intent(FactoryTestListActivity.this,LightTest.class)); break; case
		 * 23: startActivity(new
		 * Intent(FactoryTestListActivity.this,MagneticFieldTest.class)); break;
		 */
		case 23:
			intent = new Intent(FactoryTestListActivity.this,
					ProximityTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		case 24:
			intent = new Intent(FactoryTestListActivity.this,
					FlashLightTest.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			break;
		// case 26:
		// startActivity(new
		// Intent(FactoryTestListActivity.this,CPUtemperatureTest.class));
		// break;
		case 25:
			startActivity(new Intent(FactoryTestListActivity.this,
					TestResultActivity.class));
			break;
		case 26:
			//finish();
			/*intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
            intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
            startService(intent);*/
			//getActivity().sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
			/*startActivity(new Intent(FactoryTestListActivity.this,FactoryReset.class));*/
			intent = new Intent(FactoryTestListActivity.this,
					FactoryReset.class);
			intent.putExtra("groupId", idx);
			intent.putExtra("testname", mString[idx]);
			startActivity(intent);
			
			break;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateAdapter();
	}

	private void updateAdapter() {

		for (int i = 0; i < mGridViewItems.size(); i++) {
			mGridViewItems.get(i).setTestResult(
					mEngSqlite.getTestGridItemStatus(i));
		}
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mWakeLock != null) {
			mWakeLock.release();
			mWakeLock = null;
		}
		Settings.System.putInt(getContentResolver(), "IsMatTest", 0);
	}

}
// liuweibo end

